package  
{
	import flash.display.Graphics;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Graphic;
	import net.flashpunk.graphics.Image;
	
	/**
	 * ...
	 * @author Edward Russell II
	 */
	public class Weapon extends Entity 
	{
		//The current value of our weapons heat level
		public var currentHeat:Number = 0;
		
		/**
		 * 
		 * @param	source		Source for projectile image
		 * @param	fireRate	How fast to fire in shots/second
		 * @param	maxHeat		Maximum heat value for weapon
		 * @param	heatRate	How fast the weapon heats up
		 * @param	coolRate	How fast the weapon cools down
		 */
		public function Weapon(source:Class, fireRate:Number, maxHeat:Number, heatRate:Number, coolRate:Number) 
		{
			//Set the protected variabled used in this class as the passed values
			_source = source;
			_fireRate = fireRate;
			_maxHeat = maxHeat;
			_heatRate = heatRate;
			_coolRate = coolRate;
		}
		
		public function fire(_x:Number,_y:Number):void
		{
			//Sets the flag that we're over heating when our current heat is equal or greater than our max heat
			if (currentHeat >= _maxHeat) _overHeating = true
			
			
			if (!_overHeating)
			{
				//If our timer is 0
				if (_timer == 0)
				{
					//add to our current heat
					currentHeat += _heatRate;
					
					//spawn a bullet at the X and Y that were passed
					FP.world.add(new Bullet(_source, _x, _y, _scale));
				}
				
				//add the rate of the gun we're firing to the fire timer
				_timer += fireRate;
				
				//Now we set the timer back to 0 if we've added up to 60
				if (_timer > 60) _timer = 0;
			}
			
			//if we're overheating cooldown even if the player is still holding space
			if (_overHeating) cooldown();
		}
		
		public function cooldown():void
		{
			//Set the over heating flag back to false when we hit 0
			if (currentHeat == 0) _overHeating = false;
			
			//if we're overheating slowly cool down
			if (_overHeating) currentHeat -= _coolRate / 3 ;
			
			//othewise cooldown much faster
			else if (!_overHeating) currentHeat -= _coolRate;
			
			//keep our current heat from going below 0
			if (currentHeat < 0) currentHeat = 0;
		}
		
		
		/**
		 * The fire rate of the weapon
		 */
		public function get fireRate():Number { return _fireRate; }
		public function set fireRate(value:Number):void
		{
			if (_fireRate == value) return;
			_fireRate = value;
		}
		
		/**
		 * The overheating flag for the gun
		 */
		public function get overHeating():Boolean { return _overHeating; }
		public function set overHeating(value:Boolean):void
		{
			if (_overHeating == value) return;
			_overHeating = value;
		}
		
		/**
		 * The scale property for the bullet size
		 */
		public function get scale():Number { return _scale }
		public function set scale(value:Number):void
		{
			if (_scale == value) return;
			_scale = value;
		}
		
		protected var _scale:Number = 1;
		protected var _heatRate:Number;
		protected var _coolRate:Number;
		protected var _fireRate:Number;
		protected var _overHeating:Boolean = false;
		protected var _maxHeat:Number;
		protected var _timer:Number = 0;
		protected var _source:Class;
		protected var _image:Class;
		
	}
}